package com.freeland.client2009.dao.ibatis.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.freeland.client2009.dao.ibatis.HotelComputerDAO;
import com.freeland.client2009.dao.ibatis.model.AdState;
import com.freeland.client2009.dao.ibatis.model.ComputerMac;
import com.freeland.client2009.dao.ibatis.model.ComputerPowerInfo;
import com.freeland.client2009.dao.ibatis.model.NetBarInfo;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository("hotelComputerDao")
public class HotelComputerDAOImpl extends SqlMapClientDaoSupport  implements HotelComputerDAO {

	@Resource(name="sqlMapClient")
	public void setSqlMapClientFactory(SqlMapClient sqlMapClient)
	{
		this.setSqlMapClient(sqlMapClient);
	}
    public HotelComputerDAOImpl() {
        super();
    }
    /**
     * 删除酒店电脑的MAC记录
     * @param mac
     */
    public int deleteHotelMac(ComputerMac mac){
        int row =getSqlMapClientTemplate().delete("delete_hotel_mac",mac);
        return row;
    }
	 @SuppressWarnings("unchecked")
	public int updateHotelMac(String useracct,String orignMac,String newMac){
			Map map = new HashMap();
			map.put("useracct", useracct);
			map.put("orignMac", orignMac);
			map.put("newMac", newMac);
			
		 int row =getSqlMapClientTemplate().update("update_hotel_mac",map);
	     return row;
	 }
    @SuppressWarnings("unchecked")
	public List<ComputerPowerInfo> getComputerUseLogPage(int pageIndex, int pageSize,String userType,
			String useracct,String username,String startDate,String endDate,int minute){

    	Integer begin = pageIndex * pageSize;
        Integer end = (pageIndex + 1) * pageSize;
        
		Map map = new HashMap();
		map.put("begin", begin);
		map.put("end", end);
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("startDate", (startDate==null || startDate.length()==0)?null:(startDate.length()==10 ? startDate:null));
		map.put("endDate",  (endDate==null || endDate.length()==0)?null:(endDate.length()==10 ? endDate:null));
		map.put("username", (username ==null || username.length()==0)?null:username);
		map.put("minute", minute>0?minute:null);
		return getSqlMapClientTemplate().queryForList("select_ComputerPowerInfo_page", map);
		
	}
    @SuppressWarnings("unchecked")
	public Long getComputerUseLogCount(String userType,String useracct,String username,
			String startDate,String endDate,int minute){

        
		Map map = new HashMap();
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("startDate", (startDate==null || startDate.length()==0)?null:(startDate.length()==10 ? startDate:null));
		map.put("endDate",  (endDate==null || endDate.length()==0)?null:(endDate.length()==10 ? endDate:null));
		map.put("username", (username ==null || username.length()==0)?null:username);
		map.put("minute", minute>0?minute:null);
		return (Long)getSqlMapClientTemplate().queryForObject("select_ComputerPowerInfo_count", map);
		
	}
    @SuppressWarnings("unchecked")
	public List<AdState> getAdStatePage(int pageIndex, int pageSize,
			String userType, String useracct,String username) {
 
    	int begin = pageIndex * pageSize;
        int end = (pageIndex + 1) * pageSize;
        
		Map map = new HashMap();
		map.put("begin", begin);
		map.put("end", end);
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("username", (username ==null || username.length()==0)?null:username);
		return getSqlMapClientTemplate().queryForList("select_AdState_page", map);
    	
	}
    private String[] parseUseracct(String useracct){
    	if(useracct!=null){
    	    return 	useracct.split(",");
    	}
    	return null;
    }
    @SuppressWarnings("unchecked")
	public Long getAdStateCount(String userType, String useracct,String username) {
        
		Map map = new HashMap();
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("username", (username ==null || username.length()==0)?null:username);
		return (Long)getSqlMapClientTemplate().queryForObject("select_AdState_count", map);
    	
	}
    @SuppressWarnings("unchecked")
	public List<ComputerMac> getComputerMacPage(int pageIndex, int pageSize,
			String userType, String useracct,String username) {
    	Integer begin = pageIndex * pageSize;
        Integer end = (pageIndex + 1) * pageSize;
        
		Map map = new HashMap();
		map.put("begin", begin);
		map.put("useracctArray", parseUseracct(useracct));
		map.put("end", end);
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("username", (username ==null || username.length()==0)?null:username);
		
		return getSqlMapClientTemplate().queryForList("select_ComputerMac_page", map);
	}
    @SuppressWarnings("unchecked")
	public Long getComputerMacCount(String userType, String useracct,String username) {

        
		Map map = new HashMap();
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("username", (username ==null || username.length()==0)?null:username);
		
		return (Long)getSqlMapClientTemplate().queryForObject("select_ComputerMac_count", map);
	}
    @SuppressWarnings("unchecked")
	public List<NetBarInfo> getNetBarInfoPage(int pageIndex, int pageSize,
			String userType, String useracct,String username) {
    	Integer begin = pageIndex * pageSize;
        Integer end = (pageIndex + 1) * pageSize;
        
		Map map = new HashMap();
		map.put("begin", begin);
		map.put("end", end);
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		map.put("username", (username ==null || username.length()==0)?null:username);
		
		return getSqlMapClientTemplate().queryForList("select_NetBarInfo_page", map);
	}
    @SuppressWarnings("unchecked")
	public Long getNetBarInfoCount(String userType, String useracct,String username) {

        
		Map map = new HashMap();
		map.put("useracctArray", parseUseracct(useracct));
		map.put("usertype", (userType ==null || userType.length()==0)?null:userType);
		map.put("useracct", (useracct ==null || useracct.length()==0)?null:useracct);
		
		map.put("username", (username ==null || username.length()==0)?null:username);
		
		
		return (Long)getSqlMapClientTemplate().queryForObject("select_NetBarInfo_count", map);
	}
}
